package com.wan.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wan.entity.CompetitionType;
import com.wan.service.CompetitionTypeService;
import com.wan.utils.DataGridViewResult;
import com.wan.utils.SystemConstant;
import com.wan.utils.UUIDUtils;
import com.wan.vo.CompetitionTypeVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/competitionType")
public class CompetitionTypeController {

    @Resource
    private CompetitionTypeService competitionTypeService;

    /**
     * 查询比赛列表
     * @param competitionTypeVo
     * @return
     */
    @RequestMapping(value = "/list")
    public DataGridViewResult list(CompetitionTypeVo competitionTypeVo){
        //设置分页信息
        PageHelper.startPage(competitionTypeVo.getPage(),competitionTypeVo.getLimit());
        //调用分页查询的方法
        List<CompetitionType> competitionList = competitionTypeService.findCompetitionTypeList(competitionTypeVo);
        //创建分页对象
        PageInfo<CompetitionType> pageInfo = new PageInfo<>(competitionList);
        //返回数据
        return  new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/uploadFile")
    public String uploadFile(MultipartFile attach){
        //创建集合保存返回JSON数据
        Map<String,Object> map = new HashMap<>();
        //判断是否有选中的文件
        if(!attach.isEmpty()){
            //获取文件上传地址
            String path = "E:/CompetitionProject/img";
            //获取源文件的名称
            String oldFileName = attach.getOriginalFilename();
            //获取文件的后缀名
            String extension = FilenameUtils.getExtension(oldFileName);
            //重命名旧文件
            String newFileName = UUIDUtils.randomUUID()+"."+extension;
            //为了解决同一个文件夹下文件过多的问题，使用日期作为文件夹管理
            String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //组装最终的文件名
            String finalName = datePath + "/" + newFileName;
            //创建文件对象
            //参数1：文件上传的地址  参数2：文件名称
            File dest = new File(path,finalName);
            //判断该文件夹是否存在，不存在则创建文件夹
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();//创建文件夹
            }

            try {
                //进行文件上传
                attach.transferTo(dest);
                map.put("code",0);//状态码
                map.put("msg","上传成功");//执行消息
                Map<String,Object> dataMap = new HashMap<String,Object>();
                dataMap.put("src","/img/show/"+finalName);
                map.put("data",dataMap);//文件数据
                map.put("imagePath",finalName);//隐藏域的值，只保留日期文件夹+重命名后的文件名

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/addCompetitionType",produces = "text/html;charset=utf-8")
    public String addCompetitionType(CompetitionType competitionType){
        Map<String,Object> map = new HashMap<String, Object>();
        if (competitionTypeService.addCompetitionType(competitionType)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/updateCompetitionType",produces = "text/html;charset=utf-8")
    public String updateCompetitionType(CompetitionType competitionType){
        Map<String,Object> map = new HashMap<String, Object>();
        if (competitionTypeService.updateCompetitionType(competitionType)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

}
